import React from 'react';
import './styles/App.scss'
import { HashRouter, Route,Switch,Redirect } from 'react-router-dom';
//联系代理商的方法connect
import {connect} from 'react-redux'
import Home from './pages/Home'
import CitySelect from './pages/CitySelect'
import MapFound from './pages/MapFound'
import PageNotFound from './pages/PageNotFound'
import getBaiduCity from './utils/baiduMap'
// getBaiduCity().then(res=>{
//   console.log(res);
// })
class App extends React.Component {
  componentDidMount(){
    //获取定位的方法
    this.props.getCity()
  }
  state = {  }
  render() {
    console.log(this.props);
    
    return (
      <div>
        {/* 如果有城市在显示页面 */}
        {this.props.cityName && 
       <HashRouter>
         <Switch>
         <Route exact path='/'><Redirect to='/home'></Redirect></Route>
         <Route path='/home' component={Home}></Route>
         <Route path='/cityselect' component={CitySelect}></Route>
         <Route path='/mapfound' component={MapFound}></Route>
         <Route exact component={PageNotFound}></Route>
         </Switch>
       </HashRouter>
   }
      </div>
    );
  }
}
//把仓库状态映射到组件的props上
 const mapStateToProps=(state)=>({
   cityName:state.address.city
 })
 //把dispatch修改的方法映射到组件props上
 const mapDispatchToProps=(dispatch)=>({
  getCity:()=>{
  getBaiduCity().then(res=>{
    res.city=res.city.replace('市','')
    console.log(res);
    const action={
      type:'get_city',
      value:res 
    }
    dispatch(action)
  })
}
 })
export default connect(mapStateToProps, mapDispatchToProps)(App);
